/*
 * This file is part of quickReminder, a software for gamers
 *
 * Copyright (C) 2006-2008 Lucas Romero
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program; if not, see <http://www.gnu.org/licenses/>.
 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace quickReminder
{
    public partial class FormConfigViewer : Form
    {
        int line = 1;
        string cfg = string.Empty;
        public FormConfigViewer()
        {
            InitializeComponent();
            ConfigText.BackColor = Color.White;
        }
        private string AddLineNumber(Match m)
        {
            return String.Format("{0}:  {1}", ((int)line++).ToString().PadLeft(4,' '), m.Value);
        }

        public void Show(string text, int linenumber, List<ConfigKeyValue> anomalies)
        {
            line = 1;
            base.Show();
            ConfigText.Hide();
            this.Activate();
            //string newtext = Regex.Replace(text, "^.*?$", new MatchEvaluator(AddLineNumber), RegexOptions.Multiline);
            if (cfg != text)
            {
                cfg = text;
                ConfigText.Text = text;
                FocusLine(linenumber);
            
                string ConfigRegex = @"^\[/?{STEAM}\\steamapps\\{STEAMACCOUNT}\\.*?\]";
                foreach (Match m in Regex.Matches(ConfigText.Text, ConfigRegex, RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.Multiline))
                {
                    ConfigText.Select(m.Index, m.Length);
                    ConfigText.SelectionColor = Color.Blue;
                    ConfigText.SelectionFont = new Font(ConfigText.Font, FontStyle.Bold);
                }
                foreach (Match m in Regex.Matches(ConfigText.Text, "//.*?$", RegexOptions.Multiline | RegexOptions.Compiled))
                {
                    ConfigText.Select(m.Index, m.Length);
                    ConfigText.SelectionColor = Color.DarkGreen;
                    ConfigText.SelectionFont = new Font(ConfigText.Font, FontStyle.Italic);
                }
                foreach (Match m in Regex.Matches(ConfigText.Text, @"^[ \t]*?(bind|alias)", RegexOptions.Multiline | RegexOptions.IgnoreCase | RegexOptions.Compiled))
                {
                    ConfigText.Select(m.Groups[1].Index, m.Groups[1].Length);
                    ConfigText.SelectionFont = new Font(ConfigText.Font, FontStyle.Bold);
                }
                
                foreach(ConfigKeyValue k in anomalies)
                {
                    ColorLine(k);
                }
              /*  for (int i = 0; i < ConfigText.Lines.Length; i++)
                {
                    ConfigText.Lines[i] = "x: " + ConfigText.Lines[i];
                }*/
            }
            ConfigText.Show();
            
            
        }
        void ColorLine(ConfigKeyValue val)
        {
            if (val.Legality > LegalityLevel.Legal)
            {
                int start = ConfigText.GetFirstCharIndexFromLine(val.Line - 1);
                int end = ConfigText.Text.IndexOf('\n', start);
                ConfigText.Select(start, end-start);
            }
            if ((val.Legality & LegalityLevel.Illegal) > 0)
            {
                ConfigText.SelectionBackColor = Color.Red;
                ConfigText.SelectionColor = Color.White;
            }
            else if((val.Legality & LegalityLevel.Suspicious) > 0)
            {
                ConfigText.SelectionBackColor = Color.DarkOrange;
            }
        }
        void FocusLine(int line)
        {
            ConfigText.DeselectAll();
            ConfigText.Select(ConfigText.GetFirstCharIndexFromLine(line - 1), -1);
            ConfigText.DeselectAll();
        }

        private void FormConfigViewer_Load(object sender, EventArgs e)
        {

        }

        private void FormConfigViewer_FormClosing(object sender, FormClosingEventArgs e)
        {
            if(e.CloseReason == CloseReason.UserClosing)
            {
                this.Hide();
                e.Cancel = true;
            }
        }

        private void FormConfigViewer_FormClosed(object sender, FormClosedEventArgs e)
        {
            
        }
    }
}